<?php
namespace app\admin\logic;

//商品页
use think\facade\Request;
use think\facade\View;

class GoodsLogic{

    //goods 商品列表关键变色   str_replace("要变的字","变得样式","要变得全部字段")
    public static function  goodWord($goods){
        $param=Request::param();
        if (isset($param['keywords'])&& !empty($param['keywords'])){
            foreach ($goods as &$v){
                $word="<font style='color: pink;font-size:20px;'>".$param['keywords']."</font>";
                $v['goods_name']=str_replace($param['keywords'],$word,$v['goods_name']);
            }
        }

        return  $goods;
    }
    //goods 商品列表的order排序
    public static function goodOrder(){
        $param=Request::param();
        $order=['g.goods_id' => 'asc'];
        View::assign(['order'=>'1','msg'=>'']);
        if (isset($param['order']) && $param['order']=='1'){
            $order=['g.add_time'=>'desc'];
            View::assign(['order'=>'2','msg'=>'↓']);
        }else if (isset($param['order']) && $param['order']=='2'){
            $order=['g.add_time'=>'asc'];
            View::assign(['order'=>'0','msg'=>'↑']);
        }
        return $order;
    }
    //goods留值处理
    public static function   goodData(){
        $param=Request::param();
        $data=[
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'recommended'=>isset($param['recommended'])?$param['recommended']:'',
            'mintime'=>isset($param['mintime'])?$param['mintime']:'',
            'maxtime'=>isset($param['maxtime'])?$param['maxtime']:'',
            'maxmoney'=>isset($param['maxmoney'])?$param['maxmoney']:'',
            'minmoney'=>isset($param['minmoney'])?$param['minmoney']:'',
            'if_show'=>isset($param['if_show'])?$param['if_show']:[],
            'cid'=>isset($param['cid'])?$param['cid']:'',
            'brand'=>isset($param['brand'])?$param['brand']:'',
        ];
        return $data;


    }
    //goods 商品列表条件拼接
    public static function   goodWhere(){
        $where=[];
        $param=Request::param();
        if (isset($param['keywords']) && !empty($param['keywords'])){
            $where[]=['g.goods_name','like','%'.$param['keywords'].'%'];
        }
        if (isset($param['recommended']) && $param['recommended']<>''){
            $where[]=['g.recommended','=',$param['recommended']];
        }
        if (isset($param['if_show']) && count($param['if_show'])=='1'){
            $where[]=['g.if_show','in',$param['if_show']];
        }
        if (isset($param['cid']) && !empty($param['cid'])){
            $where[]=['g.cate_id','=',$param['cid']];
        }
        if (isset($param['mintime']) && !empty($param['mintime'])){
            $where[]=['g.add_time','>',strtotime($param['mintime'])];
        }
        if (isset($param['maxtime']) && !empty($param['maxtime'])){
            $where[]=['g.add_time','<',strtotime($param['maxtime'])];
        }
        if (isset($param['minmoney']) && !empty($param['minmoney'])){
            $where[]=['g.price','>',$param['minmoney']];
        }
        if (isset($param['maxmoney']) && !empty($param['maxmoney'])){
            $where[]=['g.price','<',$param['maxmoney']];
        }
        if (isset($param['brand']) && !empty($param['brand'])){
            $where[]=['g.brand','=',$param['brand']];
        }

        return $where;
    }

    //goods商品列表的下拉分类
    public static function goodCate(){
    $cate=\app\admin\model\Gcategory::cateList([]);
    $arr=[];
    foreach ($cate as $v){
        if ($v['parent_id']==0){
            $arr[$v['cate_id']]=$v;
            $arr[$v['cate_id']]['next']=self::goodCateNext($cate,$v['cate_id']);


        }
    }
   return $arr;
}
 public static function   goodCateNext($cate,$parent_id){
        $arr=[];
     foreach ($cate as $vv){
         if ($parent_id==$vv['parent_id']){
             $arr[$vv['cate_id']]=$vv;
             $arr[$vv['cate_id']]['next']=self::goodCateNext($cate,$vv['cate_id']);
         }
     }
     return $arr;
    }


   //多图时的将临时路径与老路径到数据库
     public static function   getImgsUrl($url){
         $param=Request::param();
        $old_image=[]; $new_image=[];
        if (isset($param['new_imgs'])){
            foreach ($param['new_imgs'] as $v){
                $s=explode('.',$v);
                $path_date=$url.date('Ymd/');
                $path_dir=$path_date.md5(microtime(true).rand(100000,999999)).'.'.end($s);
                $old_image[]=$path_dir;
                $new_image[]=['new'=>$path_dir,'old'=>$v];
            }
        }
         if (isset($param['old_imgs'])){
         $old_image=array_merge($param['old_imgs'], $old_image);
         }
         return [$old_image,$new_image];
     }

     //单图上传老路径加新路径
      public static function   setImgUrl($paths,$url){
        $s=explode('.',$paths);
          $path_date=$url.date('Ymd/');
          $path_dir=$path_date.md5(microtime(true).rand(100000,999999)).'.'.end($s);
          return [$path_date, $path_dir];
      }

        //多图  移动临时目录下图片到正式目录,并删除临时文件夹文件
   public static function  getImgDir($new_image,$url,$dir){
         $path_date=$url.date('Ymd/');
         if (!is_dir($dir.$path_date)){
             mkdir($dir.$path_date,0777,true);
         }
       //循环拼接的临时路径与正式路径数组
       foreach ($new_image as $v){
             copy($dir.$v['old'],$dir.$v['new']);
             unlink($dir.$v['old']);
       }

   }

   //单图移动临时目录下图片到正式目录,并删除临时文件夹文件

     public static function setImgDir($paths,$path_date,$path_dir,$dir){
            if (!is_dir($dir.$path_date)){
                mkdir($dir.$path_date,0777,true);
            }
         copy($dir.$paths,$dir.$path_dir);
         unlink($dir.$paths);
         }


}